package com.woniu.woniuproject.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.woniuproject.entity.Debt_info;
import com.woniu.woniuproject.entity.Debtor_record;
import com.woniu.woniuproject.entity.User;
import com.woniu.woniuproject.mapper.AccountMapper;
import com.woniu.woniuproject.mapper.UserMapper;
import com.woniu.woniuproject.mapper.moneyCountMapper;
import com.woniu.woniuproject.service.Debt_infoService;
import com.woniu.woniuproject.service.Debtor_recordService;
import com.woniu.woniuproject.utils.moneyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("debtinfo")
public class Debt_InfoController {
    @Autowired
    Debt_infoService debtInfoService;
    @Autowired
    Debtor_recordService debtorRecordService;

    @ResponseBody
    @RequestMapping("/list")
    public Map<String, PageInfo> list(@RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                                      @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        HashMap<String, PageInfo> map = new HashMap<>();
        PageInfo pageInfo = debtInfoService.selectList(currentPage, pageSize);
        map.put("data", pageInfo);
        return map;

    }

    @Autowired
    moneyUtil moneyUtil;
    @Autowired
    Debt_infoService debtinfoService;
    @Autowired
    AccountMapper accountMapper;
    @Autowired
    moneyCountMapper moneyCountMapper;
    @Autowired
    UserMapper userMapper;

    @ResponseBody
    @RequestMapping("/match")
    public Map<String, String> match(@RequestParam("debtYearRate") Double debtYearRate,
                                     @RequestParam("debtMoney") Double debtMoney,
                                     @RequestParam("month") int month,
                                     @RequestParam("Pid") int Pid,
                                     @RequestParam("payStyle") String  payStyle) {
        HashMap<String, String> map = new HashMap<>();
        double money = moneyUtil.changeMoney(debtYearRate, debtMoney, month, Pid, payStyle);
        if (money > 0) {
            map.put("data", "1");
            Debt_info debt_info = debtInfoService.selectById(Pid);
            String loanperiod = debt_info.getLoanperiod();
            int perid = Integer.parseInt(loanperiod);
            int years = perid / 12;
            int months = perid % 12;
            Calendar calendar = Calendar.getInstance(Locale.CHINA);
            int m1 = calendar.get(Calendar.MONTH) + 1;
            if (m1 + months > 12) {
                years++;
                months = months - 12;
            }
            Date time = calendar.getTime();
            calendar.add(Calendar.YEAR, +years);
            calendar.add(Calendar.MONTH, +months);
            calendar.add(Calendar.DAY_OF_YEAR, -1);
            Date time1 = calendar.getTime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(time);
            String format1 = sdf.format(time1);
            Date date = null;
            Date date1 = null;
            try {
                date = sdf.parse(format);
                date1 = sdf.parse(format1);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            int i = debtinfoService.updateStatus(Pid, date, date1);
            Debtor_record debtorrecord = new Debtor_record();
            debtorrecord.setClaimsid(Pid);
            debtorrecord.setYearrate(debtYearRate);
            if(payStyle=="到期一次还本付息"){
            Double Money=debtMoney*debtYearRate*month/12/100+debtMoney;
            debtorrecord.setReceivablemoney(Money);
            debtorrecord.setTotalterm("1");
            debtorrecord.setReceivabledate(date1);
            }
            else{
             Double Money=(debtMoney*debtYearRate*month/12/100+debtMoney)/month;
                debtorrecord.setReceivablemoney(Money);
                debtorrecord.setTotalterm(String.valueOf(month));
                calendar.add(Calendar.YEAR, -years);
                calendar.add(Calendar.MONTH, -months+1);
                Date time2 = calendar.getTime();
                String a1 = sdf.format(time2);
                try {
                    Date date2 = sdf.parse(a1);
                    debtorrecord.setReceivabledate(date2);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            debtorrecord.setCurrentterm("1");
            debtorrecord.setPrincipalmoney(debtMoney);
            debtorrecord.setRecorddate(date);
            debtorRecordService.insert(debtorrecord);
            String username = debt_info.getDebtorsname();
            Double matchedmoney=money;
            User user = userMapper.selectByName(username);
            Integer id = user.getId();
            accountMapper.updateByUserKey(id,matchedmoney);
            moneyCountMapper.update(matchedmoney);

            return map;
        } else {
            map.put("data", "0");
            int i = debtinfoService.updateunStatus(Pid);
            return map;
        }
    }


    @ResponseBody
    @RequestMapping("/list1")
    public Map<String, PageInfo> list1(@RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                                       @RequestParam(value = "pageSize", defaultValue = "5") int pageSize,
                                       @RequestParam(value = "startTime", defaultValue = "1900-01-01") String startTime, @RequestParam(value = "endTime", defaultValue = "2099-12-31") String endTime) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        HashMap<String, PageInfo> map = new HashMap<>();


        Date start = sdf.parse(startTime);
        Date end = sdf.parse(endTime);
        PageInfo<Debt_info> pageInfo = debtInfoService.selectApplyList(currentPage, pageSize, start, end);
        map.put("data", pageInfo);

        return map;
    }



}
